package com.ui;

import Good.Good;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.Objects;

public class Cart extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JList list1;
    private JButton Button_pur;
    private JButton Button_del;
    private JTextField textField_mon;

    public Cart() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        Button_pur.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<DENGLU.c.shopping.size();i++)
                {
                    DENGLU.c.process(DENGLU.c.Buy(DENGLU.c.shopping.get(i)));
                }
                DENGLU.c.shopping.clear();
            }
        });
        Button_del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String receiveUserNames = Arrays.toString(list1.getSelectedValues());
                System.out.println(receiveUserNames);
                String[] name=receiveUserNames.split("           ");
                for(Good g:DENGLU.c.shopping)
                {
                    if(Objects.equals('['+g.getName(), name[0]))
                    {
                        DENGLU.c.shopping.remove(g);
                    }
                }
            }
        });
    }

    private void onOK() {
        // 在此处添加您的代码
        String[] lst=new String[DENGLU.c.shopping.size()+1];
        lst[0]="商品           价格               数量";
        for(int i=1;i<=DENGLU.c.shopping.size();i++)
        {
            lst[i]=DENGLU.c.shopping.get(i-1).getName()+"           "+DENGLU.c.shopping.get(i-1).getprice()+"           "+DENGLU.c.shopping.get(i-1).getNum();
        }
        list1.setListData(lst);
        int priceall=0;
        for(int i=0;i<DENGLU.c.shopping.size();i++)
        {
            priceall+=DENGLU.c.shopping.get(i).getPrice();
        }
        textField_mon.setText(String.valueOf(priceall));
    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }

    public void main() {
        Cart dialog = new Cart();
        dialog.pack();
        dialog.setVisible(true);
        //System.exit(0);
    }
}
